package com.wushi.lenovo.asia5b.app_ui.credit.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.permissions.RxPermissions;
import com.mvp.base.BaseMvpActivity;
import com.mvp.utils.DisplayUtils;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.MemberBlanceRepository;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.CreditActivityView;
import com.wushi.lenovo.asia5b.app_di.credit.presenter.CreditActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.TopupEvent;
import com.wushi.lenovo.asia5b.app_event.TransferEvent;
import com.wushi.lenovo.asia5b.app_event.WithdrawEvent;
import com.wushi.lenovo.asia5b.app_model.credit.MemberBlanceModel;
import com.wushi.lenovo.asia5b.app_model.other.CreditType;
import com.wushi.lenovo.asia5b.app_ui.credit.adapter.CreditAdapter;
import com.wushi.lenovo.asia5b.app_utils.eye.EyeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by zhangyuncai on 2019/1/15.
 * 账务信息主界面
 */
public class CreditActivity extends BaseMvpActivity<CreditActivityView, CreditActivityPresenter> implements CreditActivityView {
    @BindView(R.id.tv_credit_time)
    TextView tv_credit_time;
    @BindView(R.id.tv_credit_yue)
    TextView tv_credit_yue;
    @BindView(R.id.rv_credit_action)
    RecyclerView mRecyclerView;
    private CreditAdapter mAdapter;

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        setStatusTrans();
        return R.layout.activity_credit;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        setMemberBalanceView();

        ArrayList<CreditType> list = new ArrayList<>();
        list.add(CreditType.topup);
//        list.add(CreditType.transfer);
        list.add(CreditType.withdraw);
        list.add(CreditType.history);
        mAdapter = new CreditAdapter(list);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mActivity, 2));
        mRecyclerView.setAdapter(mAdapter);
        GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(2, DisplayUtils.dip2px(mActivity,10), true);
        mRecyclerView.addItemDecoration(gridSpacingItemDecoration);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            CreditType type = mAdapter.getItem(position);
            switch (type) {
                case topup:
                    startActivity(new Intent(this, TopupActivity.class));
                    break;
                case transfer:
                    new RxPermissions(this).request(Manifest.permission.CAMERA).subscribe(aBoolean -> {
                        if (aBoolean) {
                            startActivity(new Intent(this, CreditTransferActivity.class));
                        } else {
                            ToastUtil.showToast(getString(R.string.permission_error1));
                        }
                    });
                    EyeUtils.tagPermission();
                    break;
                case withdraw:
                    startActivity(new Intent(this, WithdrawActivity.class));
                    break;
                case history:
                    startActivity(new Intent(this, TransHistoryActivity.class));
                    break;
                default:
                    break;
            }
        });
    }

    private void setMemberBalanceView() {
        MemberBlanceModel memberBalance = MemberBlanceRepository.getMemberBalance();
        tv_credit_yue.setText(memberBalance == null ? "0.00" : memberBalance.getBalance());
        if (!TextUtils.isEmpty(memberBalance.getLast_transaction())) {
            String lastTime = String.format(getString(R.string.credit_lasttransaction), memberBalance.getLast_transaction());
            tv_credit_time.setText(lastTime);
            tv_credit_time.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        getDatas();
    }

    private void getDatas() {
        MemberBlanceRepository.updateMemberBalance();
    }

    @Override
    protected void initEvent() {
        onFilterDoubleClick(findViewById(R.id.ll_credit_transfer));
        onFilterDoubleClick(findViewById(R.id.ll_credit_withdraw));
        Disposable subscribe = MemberBlanceRepository.getMemberBalanceOB()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(memberBlanceModel -> {
                    setMemberBalanceView();
                });
        composite.add(subscribe);
    }

    @OnClick({
            R.id.ll_credit_topup,
            R.id.ll_credit_transationhistory,
            R.id.tv_credit_add,
            R.id.rl_credit_addvessel
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_credit_addvessel://充值
            case R.id.tv_credit_add://充值
            case R.id.ll_credit_topup://充值
                startActivity(new Intent(this, TopupActivity.class));
                break;
            case R.id.ll_credit_transfer://转账
                new RxPermissions(this).request(Manifest.permission.CAMERA).subscribe(aBoolean -> {
                    if (aBoolean) {
                        startActivity(new Intent(this, CreditTransferActivity.class));
                    } else {
                        ToastUtil.showToast(getString(R.string.permission_error1));
                    }
                });
                EyeUtils.tagPermission();

                break;
            case R.id.ll_credit_withdraw://取现
                startActivity(new Intent(this, WithdrawActivity.class));
                break;
            case R.id.ll_credit_transationhistory://交易记录
                startActivity(new Intent(this, TransHistoryActivity.class));
                break;
            default:
                break;
        }

    }

    /**
     * 充值
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onTopupEvent(TopupEvent event) {
        if (event != null) {
            getDatas();
        }
    }

    /**
     * 提现
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onWithdrawEvent(WithdrawEvent event) {
        if (event != null) {
            getDatas();
        }
    }

    /**
     * 转账
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onTransferEvent(TransferEvent event) {
        if (event != null) {
            getDatas();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
